﻿using UnityEngine;
using UnityEditor;

namespace CWM.Skinn
{
    public static partial class SkinnContextCommands
    {
        [CtxMenuItem("Renderer/Clipboard/Paste/Mesh",
            "Pastes a mesh from one renderer to another",
            ContextFlag.PasteFunction, 0)]
        public static void RendererPasteMesh()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) { SkinnContext.Finished(); return; }
            var context = item.GetComponent<Renderer>();
            if (!context) { SkinnContext.Finished(); return; }
            if (!SkinnContext.CopyRef.Asset) return;
            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (SkinnEx.IsNullOrNotVailid(copyContext)) { SkinnContext.Finished(); return; }
            var copyMesh = copyContext.GetSharedMesh();

            SkinnContext.UndoRecord(context, "Paste Mesh");
            context.SetSharedMesh(copyMesh);

            SkinnContext.Finished();
        }

        [CtxMenuItem("Renderer/Clipboard/Paste/Mesh Cloned",
            "Pastes a mesh from one renderer to another and instances the pasted mesh.",
            ContextFlag.PasteFunction, 0)]
        public static void RenderePasteMeshNew()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) { SkinnContext.Finished(); return; }
            var context = item.GetComponent<Renderer>();
            if (!context) { SkinnContext.Finished(); return; }
            if (!SkinnContext.CopyRef.Asset) return;
            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (SkinnEx.IsNullOrNotVailid(copyContext)) { SkinnContext.Finished(); return; }
            var copyMesh = copyContext.GetSharedMesh();

            SkinnContext.UndoRecord(context, "Paste Mesh");
            context.SetSharedMesh(copyMesh.Clone() as Mesh);

            SkinnContext.Finished();
        }


        [CtxMenuItem("Renderer/Clipboard/Paste/Material(s)",
            "Pastes shared materials from one renderer to another.",
            ContextFlag.PasteFunction, 0)]
        public static void RendererPasteMaterials()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) { SkinnContext.Finished(); return; }
            var context = item.GetComponent<Renderer>();
            if (!context) { SkinnContext.Finished(); return; }
            if (!SkinnContext.CopyRef.Asset) { SkinnContext.Finished(); return; }
            var copyContext = (Renderer)SkinnContext.CopyRef.Asset;
            if (SkinnEx.IsNullOrNotVailid(copyContext)) { SkinnContext.Finished(); return; }
            SkinnContext.UndoRecord(context, "Paste Material");
            context.sharedMaterials = copyContext.sharedMaterials;

            SkinnContext.Finished();
        }

        [CtxMenuItem("SkinnedMeshRenderer/Clipboard/Paste/Bones",
            "Pastes a Transform array from one skinned mesh renderer to another.",
            ContextFlag.PasteFunction, 0)]
        public static void RendererPasteBones()
        {
            var item = SkinnContext.GetContextItem();
            if (item && SkinnEx.IsNullOrNotInAScene(item.transform)) { SkinnContext.Finished(); return; }
            var context = item.GetComponent<SkinnedMeshRenderer>();
            if (!context) { SkinnContext.Finished(); return; }
            if (!SkinnContext.CopyRef.Asset) { SkinnContext.Finished(); return; }
            var copyContext = (SkinnedMeshRenderer)SkinnContext.CopyRef.Asset;
            if (!copyContext) { SkinnContext.Finished(); return; }
            SkinnContext.UndoRecord(context, "Paste Bones");
            context.rootBone = copyContext.rootBone;
            context.bones = copyContext.bones;

            SkinnContext.Finished();
        }
    }
}